package 力扣日常刷题.木22二月.第04天210;

import 力扣日常刷题.TreeNode;

/**
 * @author 帅小伙
 * @date 2022/2/10
 * @description
 * 1. 排序列表
 * 2. 双向链表
 */
public class Demo37二叉搜索树与双向链表 {

    TreeNode pre, head;

    public TreeNode treeToDoublyList(TreeNode root) {
        if (root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    void dfs(TreeNode cur) {
        if(cur == null) return;
        dfs(cur.left);
        // 当pre为空   代表当前访问的是头节点
        if(pre != null) pre.right = cur;
        else head = cur;
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }

}
